package com.kunbo.cn.service.impl.core.purchase;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.app.util.EmtryUtil;
import com.kunbo.cn.dao.core.purchase.WaybillDao;
import com.kunbo.cn.entity.core.purchase.Waybill;
import com.kunbo.cn.service.core.purchase.WaybillService;

@Service
public class WaybillServiceImpl implements WaybillService {
	@Autowired
	private WaybillDao waybillDao;
	@Autowired
	private JdbcTemplate jdbc;

	@Override
	public void save(Waybill waybill) {
		waybillDao.save(waybill);

	}

	@Override
	public void update(Waybill waybill) {
		waybillDao.save(waybill);

	}

	@Override
	public void delete(String id) {
		waybillDao.delete(id);

	}

	@Override
	public Waybill findById(String id) {
		// TODO Auto-generated method stub
		return waybillDao.findOne(id);
	}

	@Override
	public List<Map<String, Object>> getAllOrder() {
		// TODO Auto-generated method stub
		String sql = "select purchase_order_code from bussiness_purchasing_order_manager";
		return jdbc.queryForList(sql);
	}

	@Override
	public String getMaterielName(String order_code) {
		// TODO Auto-generated method stub
		String a = "select material_name from bussiness_purchasing_order_manager where  id = "
				+ Integer.parseInt(order_code);
		List<Map<String, Object>> list = jdbc.queryForList(a);
		for (Map<String, Object> map : list) {
			if (map.get("material_name").toString() != null && !map.get("material_name").toString().equals("")) {
				String sql = "select name from business_materials_manage where id = "
						+ Integer.parseInt(map.get("material_name").toString());
				list = jdbc.queryForList(sql);
				for (Map<String, Object> map2 : list) {
					return map2.get("name").toString();
				}
			}
		}

		return null;
	}

	@Override
	public String computationAllowance(String orderCode, float freightVolume, String judge) {
		// TODO Auto-generated method stub
		// judge控制修改的量是否低于之前的量
		String sqlShow = "select allowance from bussiness_purchasing_order_manager where purchase_order_code = '"
				+ orderCode + "'";
		List<Map<String, Object>> map = jdbc.queryForList(sqlShow);
		double allowance = Double.parseDouble(map.get(0).get("allowance").toString());
		double a = allowance - freightVolume;
		String sql = null;
		if (judge.equals("true")) {
			if (a == 0 || a < 0) {
				sql = "update bussiness_purchasing_order_manager set allowance = 0  where  purchase_order_code = '"
						+ orderCode + "'";
			} else {
				sql = "update bussiness_purchasing_order_manager set allowance = " + a
						+ "  where  purchase_order_code = '" + orderCode + "'";
			}
		} else {
			sql = "update bussiness_purchasing_order_manager set allowance = allowance +  " + freightVolume
					+ " where purchase_order_code = '" + orderCode + "'";

		}
		System.out.println(sql);
		int i = jdbc.update(sql);
		if (i > 0)
			return "true";
		else
			return "false";
	}

	@Override
	public String getOrderIdGetMaterial(Integer id) {
		// TODO Auto-generated method stub
		String orderId = "select material_name from bussiness_purchasing_order_manager where id = " + id;
		List<Map<String, Object>> list = jdbc.queryForList(orderId);
		for (Map<String, Object> map : list) {
			if (map.get("material_name").toString() != null && !map.get("material_name").toString().equals("")) {
				String materialName = "select name from business_materials_manage where id = "
						+ Integer.parseInt(map.get("material_name").toString());
				list = jdbc.queryForList(materialName);
				for (Map<String, Object> map2 : list) {
					return map2.get("name").toString();
				}
			}
		}
		return null;
	}

	@Override
	public Boolean orderTransfer(String orderId, String orderIds, Float number) {
		// TODO Auto-generated method stub
		String materialSql = "select material_name from bussiness_purchasing_order_manager where purchase_order_code = '"
				+ orderId + "'";
		String materialsSql = "select material_name from bussiness_purchasing_order_manager where purchase_order_code = '"
				+ orderIds + "'";
		List<Map<String, Object>> material = jdbc.queryForList(materialSql);
		List<Map<String, Object>> materials = jdbc.queryForList(materialsSql);
		if (material != null && materials != null) {
			String aa = material.get(0).get("material_name").toString();
			String b = materials.get(0).get("material_name").toString();
			System.out.println(aa + "=========" + b);
			if ((aa != null && !aa.equals("")) && (b != null && !b.equals(""))) {
				if (aa.equals(b)) {
					String sqlShow = "select allowance from bussiness_purchasing_order_manager where purchase_order_code = '"
							+ orderId + "'";
					List<Map<String, Object>> map = jdbc.queryForList(sqlShow);
					double allowance = Double.parseDouble(map.get(0).get("allowance").toString());
					double a = allowance - number;
					System.out.println(a);
					String sql = "";
					if (a == 0 || a < 0) {
						sql = "update bussiness_purchasing_order_manager set allowance = 0  where purchase_order_code = '"
								+ orderId + "'";
					} else {
						sql = "update bussiness_purchasing_order_manager set allowance =  " + a
								+ " where purchase_order_code = '" + orderId + "'";
					}
					System.out.println(sql);
					String sqls = "update bussiness_purchasing_order_manager set allowance = allowance + " + number
							+ " where purchase_order_code = '" + orderIds + "'";
					System.out.println(sqls);
					int i = jdbc.update(sql);
					if (i > 0) {
						i = jdbc.update(sqls);
					}
					if (i > 0)
						return true;
					else
						return false;
				}
			}

		}
		return false;

	}

	@Override
	public List<Map<String, Object>> getWayBillOrder(String wayBillCode, Integer wayBillState, Integer page,
			Integer pageSize, Integer wayBillType, Integer wayBillUnit) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String unit = "";
		String unitSql = "select name_of_transport_unit from transport_unit_management where id = " + wayBillUnit;
		if (jdbc.queryForList(unitSql) != null && jdbc.queryForList(unitSql).size() > 0) {
			unit = jdbc.queryForList(unitSql).get(0).get("name_of_transport_unit").toString();
		}
		if (wayBillType == 1) {
			// 采购运单----------
			String sql = "select * from business_purchase_waybill where 1 = 1 ";
			if (wayBillCode != null && !wayBillCode.equals("")) {
				sql += " and purchase_waybill_code like '%" + wayBillCode + "%'";
			}
			sql += " and purchase_waybill_entrustedunit = '" + unit + "'";
			if (wayBillState != null && wayBillState > 0) {
				sql += " and  purchase_waybill_state = " + wayBillState;
			}
			sql += " order by id desc limit " + (page * pageSize) + "," + pageSize;
			
			list = jdbc.queryForList(sql);
			for (Map<String, Object> map : list) {
				double a = Double.parseDouble(map.get("purchase_waybill_freightvolume").toString());
				double b = Double.parseDouble(map.get("purchase_waybill_stayfreightvolume").toString());
				double c = (a - b) / a * 1;
				DecimalFormat df = new DecimalFormat("#.##%");
				map.put("transportRate", df.format(c));
				/*
				 * String orderNumber =
				 * "select purchase_order_code from bussiness_purchasing_order_manager where id = "
				 * + Integer.parseInt(map.get("purchase_waybill_source").toString()); if
				 * (jdbc.queryForList(orderNumber) != null &&
				 * jdbc.queryForList(orderNumber).size() > 0) {
				 * map.put("purchase_waybill_source",
				 * jdbc.queryForList(orderNumber).get(0).get("purchase_order_code").toString());
				 * } else { map.put("purchase_waybill_source", null); }
				 * 
				 * String wayBillStateSql = "select text  from select_waybill_order where id = "
				 * + Integer.parseInt(map.get("purchase_waybill_state").toString()); if
				 * (jdbc.queryForList(wayBillStateSql) != null &&
				 * jdbc.queryForList(wayBillStateSql).size() > 0) {
				 * map.put("purchase_waybill_state",
				 * jdbc.queryForList(wayBillStateSql).get(0).get("text").toString()); } else {
				 * map.put("purchase_waybill_state", null); }
				 * 
				 * String entrustedUnitSql =
				 * "select  transport_unit_number from transport_unit_management where id = " +
				 * Integer.parseInt(map.get("purchase_waybill_entrustedunit").toString()); if
				 * (jdbc.queryForList(entrustedUnitSql) != null &&
				 * jdbc.queryForList(entrustedUnitSql).size() > 0) {
				 * map.put("purchase_waybill_entrustedunit",
				 * jdbc.queryForList(entrustedUnitSql).get(0).get("transport_unit_number").
				 * toString()); } else { map.put("purchase_waybill_entrustedunit", null); }
				 * 
				 * String placeoftransportationSql =
				 * "select name  from  business_customer_manage  where id = " +
				 * Integer.parseInt(map.get("purchase_waybill_placeoftransportation").toString()
				 * ); if (jdbc.queryForList(placeoftransportationSql) != null &&
				 * jdbc.queryForList(placeoftransportationSql).size() > 0) {
				 * map.put("purchase_waybill_placeoftransportation",
				 * jdbc.queryForList(placeoftransportationSql).get(0).get("name").toString()); }
				 * else { map.put("purchase_waybill_placeoftransportation", null); }
				 */
			}
		} else {
			// 销售运单--------------
			String saleSql = "select id,customer,trans_total as purchase_waybill_freightvolume , capacity_to_be_transported as purchase_waybill_stayfreightvolume,waybill_code as purchase_waybill_code, transit_compy as purchase_waybill_entrustedunit ,supplier as  purchase_waybill_placeoftransportation ,order_code as purchase_waybill_source ,material as purchase_waybill_materiel , supplier as  purchase_waybill_placeoftransportation  from business_waybill_manage where 1 = 1";
			if (EmtryUtil.notEmpty(wayBillCode)) {
				saleSql += " and waybill_code like '%" + wayBillCode + "%'";
			}
			if (wayBillState != null && wayBillState > 0) {
				saleSql += " and state = '" + wayBillState + "'";
			}
			saleSql += " and transit_compy = '" + unit + "'";
			saleSql += " order by id desc limit " + (page * pageSize) + "," + pageSize;
			list = jdbc.queryForList(saleSql);
			for (Map<String, Object> map : list) {
				double a = 0;
				if (map.get("purchase_waybill_freightvolume") != null) {
					a = Double.parseDouble(map.get("purchase_waybill_freightvolume").toString());
				}
				double b = 0;
				if (map.get("purchase_waybill_stayfreightvolume") != null) {
					b = Double.parseDouble(map.get("purchase_waybill_stayfreightvolume").toString());
				}
				double c = (a - b) / a * 1;
				DecimalFormat df = new DecimalFormat("#.##%");
				map.put("transportRate", df.format(c));
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count", list.size());
		list.add(map);
		return list;
	}

	@Override
	public List<Map<String, Object>> indexData(String waybillEntrustedunit) {
		// TODO Auto-generated method stub
		float a = 0, b = 0, c = 0, d = 0;
		String sql = " select * from transport_unit_management where name_of_transport_unit = '" + waybillEntrustedunit
				+ "'";
		if (jdbc.queryForList(sql) != null && jdbc.queryForList(sql).size() > 0) {
			String sqlPurchaseSum = "select sum(purchase_waybill_freightvolume) as `sum` ,sum(purchase_waybill_freightvolume-purchase_waybill_stayfreightvolume) as `sum1`, sum(purchase_waybill_stayfreightvolume) as `sum2` from business_purchase_waybill where purchase_waybill_entrustedunit ='"
					+ waybillEntrustedunit +"'"
					+ " and purchase_waybill_state = '" + 4 + "'";
			String sqlSaleSum = "select sum(trans_total) as `sum`,sum(trans_total-capacity_to_be_transported) as `sum1`,sum(capacity_to_be_transported) as `sum2`  from business_waybill_manage where transit_compy = '"
					+ waybillEntrustedunit + "' and state = '" + 4 + "'";
			System.out.println(sqlPurchaseSum);
			System.out.println(sqlSaleSum);
			List<String> sqls = new ArrayList<String>();
			sqls.add(sqlPurchaseSum);
			sqls.add(sqlSaleSum);
			for (int i = 0; i < sqls.size(); i++) {
				if (jdbc.queryForList(sqls.get(i)) != null || jdbc.queryForList(sqls.get(i)).size() > 0) {
					if (jdbc.queryForList(sqls.get(i)).get(0).get("sum") != null) {
						a += Float.parseFloat(jdbc.queryForList(sqls.get(i)).get(0).get("sum").toString());
					}
					if (jdbc.queryForList(sqls.get(i)).get(0).get("sum1") != null) {
						b += Float.parseFloat(jdbc.queryForList(sqls.get(i)).get(0).get("sum1").toString());
					}
					if (jdbc.queryForList(sqls.get(i)).get(0).get("sum2") != null) {
						c += Float.parseFloat(jdbc.queryForList(sqls.get(i)).get(0).get("sum2").toString());
					}
				}

			}
		}
		Map<String, Object> map = new HashMap<>();
		map.put("a", a);
		map.put("b", b);
		map.put("c", c);
		List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
		lists.add(map);
		return lists;
	}

	@Override
	public String getOrderCode(Integer waybillId) {
		// TODO Auto-generated method stub
		String sqls = "select purchase_waybill_source from business_purchase_waybill where id = " + waybillId;
		String sql = "select purchase_order_code from bussiness_purchasing_order_manager where id = "
				+ Integer.parseInt(jdbc.queryForList(sqls).get(0).get("purchase_waybill_source").toString());
		return jdbc.queryForList(sql).get(0).get("purchase_order_code").toString();
	}

	@Override
	public String getMaterielNames(String getMaterielName) {
		// TODO Auto-generated method stub
		String a = "select material_name from bussiness_purchasing_order_manager where  purchase_order_code = '"
				+ getMaterielName + "'";
		List<Map<String, Object>> list = jdbc.queryForList(a);
		for (Map<String, Object> map : list) {
			if (map.get("material_name").toString() != null && !map.get("material_name").toString().equals("")) {
				String sql = "select name from business_materials_manage where id = "
						+ Integer.parseInt(map.get("material_name").toString());
				list = jdbc.queryForList(sql);
				for (Map<String, Object> map2 : list) {
					return map2.get("name").toString();
				}
			}
		}

		return null;
	}

	@Override
	public float getallowance(Integer id) {
		// TODO Auto-generated method stub
		String sql = "select allowance from bussiness_purchasing_order_manager where id = " + id;
		float a = Float.parseFloat(jdbc.queryForList(sql).get(0).get("allowance").toString());
		return a;
	}

	@Override
	public boolean waybillOrderCount(String id, float count, String state) {
		// TODO Auto-generated method stub
		String sql = "select purchase_waybill_stayfreightvolume from business_purchase_waybill where purchase_waybill_code = '"
				+ id + "'";
		float a = 0;
		if (jdbc.queryForList(sql) != null && jdbc.queryForList(sql).size() > 0) {
			if (jdbc.queryForList(sql).get(0).get("purchase_waybill_stayfreightvolume") != null) {
				a = Float
						.parseFloat(jdbc.queryForList(sql).get(0).get("purchase_waybill_stayfreightvolume").toString());
			}
			String updateSql = null;
			if (state.equals("true")) {
				if (a - count > 0 || a - count == 0) {
					updateSql = " update business_purchase_waybill set purchase_waybill_stayfreightvolume = "
							+ (a - count) + " where purchase_waybill_code = '" + id + "'";
				} else {
					updateSql = " update business_purchase_waybill set purchase_waybill_stayfreightvolume = " + 0
							+ " where purchase_waybill_code = '" + id + "'";
				}
			} else {
				updateSql = " update business_purchase_waybill set purchase_waybill_stayfreightvolume = purchase_waybill_stayfreightvolume + "
						+ count + " where purchase_waybill_code = '" + id + "'";
				/*String updateSqls = "update business_notice_of_arrival set accruals = 0 where id = " + arriavlId ;
				jdbc.update(updateSqls);*/
			}
			int i = jdbc.update(updateSql);
			if (i > 0)
				return true;
			else
				return false;
		}
		return true;
	}

	@Override
	public Boolean updateWayBillStatus(String tableName, Integer id, String stateCode) {
		// TODO Auto-generated method stub
		String sql = null;
		if (tableName.equals("business_purchase_waybill")) {
			sql = "update " + tableName + " set purchase_waybill_state = '" + stateCode + "' where id = " + id;
		} else if (tableName.equals("business_notice_of_arrival")) {
			sql = "update " + tableName + " set notice_state = '" + stateCode + "' where id = " + id;
		} else if(tableName.equals("bussiness_purchasing_order_manager")) {
			sql = "update " + tableName + " set order_state = '" + stateCode + "' where id = " + id;
		}
		if(sql != null) {
			if (jdbc.update(sql) > 0)
				return true;
			else
				return false;
		}else {
			return false;
		}
	}

}
